###############################################################################
# Appendix Figure 9 1a / 2a
###############################################################################
# 
###############################################################################
# Content
###############################################################################
# 1) Dependencies
# 2) Load Data
# 3) Aggregation for Figure
# 4) Figure
###############################################################################
# 1) Dependencies
###############################################################################
library(readr)
library(dplyr)
library(plyr)
library(ggplot2)
library(gganimate)
library(ggeffects)
library(ggExtra)
library(ggridges)
library(ggrepel)
library(grid)
library(scales)
library(lubridate)
library(extrafont)
library(reshape2)
library(here)
library(ggforce)
library(png)
library(readxl)
library(grid)
library(gridExtra)
library(ggpubr)
library(ggalt)
library(stringr)
###############################################################################
# 2) Load Data
###############################################################################
# Set Path
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
rm(list=ls())

# Custom functions
# ggplot rescale x axis....
scale_x_reordered <- function(..., sep = "___") {
  reg <- paste0(sep, ".+$")
  ggplot2::scale_x_discrete(labels = function(x) gsub(reg, "", x), ...)
}
# ggplot order over facets...
reorder_within <- function(x, by, within, fun = mean, sep = "___", ...) {
  new_x <- paste(x, within, sep = sep)
  stats::reorder(new_x, by, FUN = fun)
}

suppressWarnings(source('ggplot_theme_ddl.R', encoding = "UTF-8"))

# Load Candidates Lists (full lists not just the one we found in the newsarticles)
candidates_list_15 <- read_csv("../support/candidates-2015/candidates_list_final_2015.csv") %>% mutate(gender = tolower(gender))
candidates_list_19 <- read_csv("../support/candidates-2019/candidates_list_final_2019.csv")  %>% mutate(gender = tolower(gender))

# Load Candidates Lists (full lists not just the one we found in the newsarticles)
bfs_list_2019 <- read_delim("../support/parliament/frauenanteil_nr_zeitreihe_2.csv", delim = ";")
###############################################################################
# 3) Aggregations for Figure
###############################################################################
agg_15 <- candidates_list_15 %>% dplyr::group_by(party,gender) %>%
                                 dplyr::summarise(n = n()) %>%
                                 dplyr::mutate(f = n/sum(n),
                                               year = "2015")
                                                            
agg_15 %>% ungroup %>% dplyr::group_by(gender) %>% dplyr::summarise(f = mean(f))


agg_19 <- candidates_list_19 %>% dplyr::group_by(party,gender) %>%
                                 dplyr::summarise(n = n()) %>%
                                 dplyr::mutate(f = n/sum(n),
                                               year = "2019")
agg_19 %>% ungroup %>% dplyr::group_by(gender) %>% dplyr::summarise(f = mean(f))


agg_1 <- dplyr::bind_rows(agg_15,agg_19) %>% filter(gender == "f")

agg2 <- bfs_list_2019 %>% filter(kanton_bezeichnung == "CH") %>% 
  filter(partei_bezeichnung_de %in% c("SVP","SP","CVP","FDP","GPS","GLP","BDP")) %>%
  group_by(wahl_jahr,partei_bezeichnung_de) %>% mutate(f_kand = anzahl_kandidierende_f / anzahl_kandidierende,
                                 f_gewh = anzahl_gewaehlte_f / anzahl_gewaehlte)

agg2 <- agg2 %>% pivot_longer(cols = c(f_kand, f_gewh), names_to = "Type") %>% 
  mutate(Type = ifelse(Type == "f_gewh", "Elected Parliamentarians", "Candidates"))
###############################################################################
# 4) Figure
###############################################################################
values_year <- c("2019" = "#DD2461", "2015" = "#7D7D7C")
values_type <- c("Elected Parliamentarians" = "#DD2461", "Candidates" = "#7D7D7C")


agg_1 <- agg_1 %>% mutate(party = factor(party, levels = agg_1 %>% 
                                         filter(year == 2019) %>%  # filter for the year 2019
                                         arrange(desc(f)) %>%  # arrange by f in descending order
                                         pull(party)))

fig_A <- ggplot(data = agg_1, aes(x=party,y=f, color = year, fill = year, group = year)) +
          geom_bar(stat = "identity", position = position_dodge(width = 1), width = .75) +
          #facet_wrap(~ year,  scales = "free_x") +
          labs(x = "", y = "Share of Female Candidates [%]", title = "Female Candidates Share by Parties in 2015 and 2019", color = "Year:", fill = "Year:") +
          scale_x_reordered(expand = c(0,0)) + 
          scale_y_continuous(labels = percent_format(scale = 100), breaks = seq(0,.5,by=.1)) +
          scale_color_manual(values=values_year) +
          scale_fill_manual(values=values_year) +
          ddl_theme(type = 'default',
                    panel.grid.major=element_blank(),
                    legend.position='none',
                    axis.line.y.left = element_line(colour="black"),
                    axis.line.x.bottom = element_line(colour="black")) +
          theme(legend.position = "bottom", legend.direction = "horizontal",
                strip.background = element_blank(), strip.text = element_text(color = "black"),
                axis.text.x = element_text(angle = 0, hjust = .5, vjust = 1, size = 16),
                axis.text.y = element_text(hjust=.5, size = 16),
                strip.text.x = element_text(size = 16),
                axis.title = element_text(size = 16),
                plot.title = element_text(size = 20),
                legend.text = element_text(size = 16),
                plot.margin = unit(c(.5,1.3,.5,.5), "cm"),
                legend.key.size = unit(1.5,"line"),
                axis.line.x = element_line(color="black", size = .5),
                axis.line.y = element_line(color="black", size = .5),
                panel.spacing.x=unit(2.5, "lines"))

fig_A

fig_B <- ggplot(data = agg2, aes(x=wahl_jahr,y=value,fill=Type, color=Type)) +
  geom_bar(stat = "identity", position = position_dodge(width = 1.9), width = 1.5) +
  labs(x = "", y = "Share of Female Candidates [%]", title = "Female Candidates over Time in the National Council", color = "Type:", fill = "Type:") +
  scale_y_continuous(labels = percent_format(scale = 100), breaks = seq(0,.65,by=.05)) +
  scale_x_continuous(breaks = c(1991,1995,1999,2003,2007,2011,2015,2019), limits = c(1990,2020)) +
  scale_color_manual(values=values_type) +
  scale_fill_manual(values=values_type) +
  facet_wrap(~partei_bezeichnung_de, scales = "free_x", ncol = 2) +
  ddl_theme(type = 'default',
            panel.grid.major=element_blank(),
            legend.position='none',
            axis.line.y.left = element_line(colour="black"),
            axis.line.x.bottom = element_line(colour="black")) +
  theme(legend.position = "bottom", legend.direction = "horizontal",
        strip.background = element_blank(), strip.text = element_text(color = "black"),
        axis.text.x = element_text(angle = 0, hjust = .5, vjust = 1, size = 16),
        axis.text.y = element_text(hjust=.5, size = 16),
        strip.text.x = element_text(size = 16),
        axis.title = element_text(size = 16),
        plot.title = element_text(size = 20),
        legend.text = element_text(size = 16),
        plot.margin = unit(c(.5,1.3,.5,.5), "cm"),
        legend.key.size = unit(1.5,"line"),
        axis.line.x = element_line(color="black", size = .5),
        axis.line.y = element_line(color="black", size = .5),
        panel.spacing.x=unit(2.5, "lines"))

fig_B



ggsave(plot = fig_A, filename ='../img_appendix/afigure_9_1a.png',width=12, height=12, dpi = 300, bg = "white")
ggsave(plot = fig_B, filename ='../img_appendix/afigure_9_2a.png',width=12, height=17, dpi = 300, bg = "white")
